package cn.qn.controller;

import cn.qn.base.PageVO;
import cn.qn.common.util.UserSubjectUtil;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.user.*;
import cn.qn.exceptions.BizException;
import cn.qn.moudle.user.AppUser;
import cn.qn.service.IAppUserService;
import cn.qn.service.user.AppUserScoreService;
import cn.qn.vo.AppUserVo;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Bryant
 * @date: 2019/7/15
 * @description:
 */
@RestController
@RequestMapping("/user/score")
public class AppUserScoreController {

    @Reference(check = false,timeout = 30000)
    private AppUserScoreService appUserScoreService;

    @RequestMapping(value = "/check-in", name = "签到领取积分", method = RequestMethod.POST)
    public ResponseDTO checkIn(@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        if (null == appUser) {
            return ResponseDTO.error("登录已过期");
        }
        try {
           Integer score = appUserScoreService.checkIn(appUser.getUserId());
            return ResponseDTO.success(score);
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/list" ,name="app积分获取记录接口",method = RequestMethod.GET)
    public ResponseDTO list( AppUserScoreQueryDTO query,@RequestHeader String authorization){
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        if (null == appUser) {
            return ResponseDTO.error("登录已过期");
        }
        query.setUserId(appUser.getUserId());
        PageVO<AppUserScoreDTO> scores = appUserScoreService.pageQueryAppUserScores(query);
        return ResponseDTO.success(scores);
    }
}
